package com.woniu.stafforder.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MoneyToCNFormat {
    private static final Pattern AMOUNT_PATTERN = Pattern.compile("^-?(0|[1-9]\\d{0,15})(\\.\\d+)?$"); // 不考虑分隔符的正确性
    private static final String[] CN_NUMBERS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static final String[] CN_MONETETARY_UNIT = {"分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾",
            "佰", "仟", "兆", "拾", "佰", "仟"};
    private static final String CN_FULL = "整";
    private static final String CN_NEGATIVE = "负";
    private static final String CN_ZERO = "零角零分";
    private static final long MAX_NUMBER = 1000000000000000l;// 最大15位整数
    private static final String INVALID_AMOUNT = "金额超出最大金额百兆亿(15位整数)";

    public static String formatToCN(String number) {
        // 去掉分隔符
        number = number.replace(",", "");

        // 验证金额正确性
        Matcher matcher = AMOUNT_PATTERN.matcher(number);
        if (!matcher.find()) {
            throw new IllegalArgumentException(number + "：输入金额有误.");
        }

        double amount = Double.parseDouble(number);
        if (Math.abs(amount) >= MAX_NUMBER)
            return INVALID_AMOUNT;
        StringBuilder result = new StringBuilder();
        long value = Math.abs(Math.round(amount * 100));
        int i = 0;
        while (true) {
            int temp = (int) (value % 10);
            result.insert(0, CN_MONETETARY_UNIT[i]);
            result.insert(0, CN_NUMBERS[temp]);
            value = value / 10;
            if (value < 1)
                break;
            i++;
        }
        // "零角零分" 转换成 "整"
        int index = result.indexOf(CN_ZERO);
        if (index > -1) {
            result.replace(index, index + 4, CN_FULL);
        }
        // 负数
        if (amount < 0) {
            result.insert(0, CN_NEGATIVE);
        }
        return result.toString();
    }
}
